﻿Public Class frmLineSet

    Private m_ModName As String = "frmLineSet"
    Private lineid As String = ""
    Private linename As String = ""
    Public m_Insight(4) As Cognex.InSight.CvsInSight
    Private saveflag As Integer = 0

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        If (Me.lineid = "1") Then
            If MsgBox("确认要保存一号生产线的数据？", MsgBoxStyle.YesNo, "生产线配置") = MsgBoxResult.Yes Then
                saveLineInfo()
            End If
        ElseIf (Me.lineid = "2") Then
            If MsgBox("确认要保存二号生产线的数据？", MsgBoxStyle.YesNo, "生产线配置") = MsgBoxResult.Yes Then
                saveLineInfo()
            End If
        ElseIf (Me.lineid = "3") Then
            If MsgBox("确认要保存三号生产线的数据？", MsgBoxStyle.YesNo, "生产线配置") = MsgBoxResult.Yes Then
                saveLineInfo()
            End If
        ElseIf (Me.lineid = "4") Then
            If MsgBox("确认要保存四号生产线的数据？", MsgBoxStyle.YesNo, "生产线配置") = MsgBoxResult.Yes Then
                saveLineInfo()
            End If
        End If
    End Sub

    Private Sub btnLine1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine1.Click
        lineid = "1"
        Me.lbTitle.Text = "当前是一号生产线配置信息"
        loadLineInfo()
    End Sub

    Private Sub frmLineSet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lbTitle.Text = ""
        Me.btnConfirm.Enabled = False
    End Sub

    Private Sub btnLine2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine2.Click
        lineid = "2"
        Me.lbTitle.Text = "当前是二号生产线配置信息"
        loadLineInfo()
    End Sub

    Private Sub btnLine3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine3.Click
        lineid = "3"
        Me.lbTitle.Text = "当前是三号生产线配置信息"
        loadLineInfo()
    End Sub

    Private Sub btnLine4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine4.Click
        lineid = "4"
        Me.lbTitle.Text = "当前是四号生产线配置信息"
        loadLineInfo()
    End Sub

    Private Sub loadLineInfo()
        Dim funName As String = "loadLineInfo"
        stlineinfo.lineid = lineid
        Me.btnConfirm.Enabled = True
        Try
            Dim sqlStr As String = "select * from t_lineinfo where belong_lineid='" & lineid & "'"
            Dim dt As DataTable = execSQLQuery(sqlStr)
            Dim CycI As Integer

            For CycI = 0 To dt.Rows.Count - 1
                Select Case dt.Rows(CycI).Item("param_type").ToString
                    Case "job"
                        Select Case dt.Rows(CycI).Item("param_name").ToString
                            Case "jobname"
                                Me.cbJob.Text = dt.Rows(CycI).Item("param_value")
                                stlineinfo.job = dt.Rows(CycI).Item("param_value")
                        End Select
                    Case "plc"
                        Select Case dt.Rows(CycI).Item("param_name").ToString
                            Case "kickparam"
                                Me.nudKickParam.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.kickparam = CInt(dt.Rows(CycI).Item("param_value"))
                        End Select
                    Case "onlinecounter"
                        Select Case dt.Rows(CycI).Item("param_name").ToString
                            Case "stackTopNum"
                                Me.nudStackTopNum.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.stackTopNum = CInt(dt.Rows(CycI).Item("param_value"))
                        End Select
                    Case "cameraparam"
                        Select Case dt.Rows(CycI).Item("param_name").ToString
                            Case "param1"
                                Me.nudCameraParam1.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.cameraparam1 = dt.Rows(CycI).Item("param_value")
                            Case "param2"
                                Me.nudCameraParam2.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.cameraparam2 = dt.Rows(CycI).Item("param_value")
                            Case "param3"
                                Me.nudCameraParam3.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.cameraparam3 = dt.Rows(CycI).Item("param_value")
                            Case "param4"
                                Me.nudCameraParam4.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.cameraparam4 = dt.Rows(CycI).Item("param_value")
                            Case "param5"
                                Me.nudCameraParam5.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.cameraparam5 = dt.Rows(CycI).Item("param_value")
                            Case "param6"
                                Me.nudCameraParam6.Value = CInt(dt.Rows(CycI).Item("param_value"))
                                stlineinfo.cameraparam6 = dt.Rows(CycI).Item("param_value")
                            Case "ip"
                                Me.txtCameraIP.Text = dt.Rows(CycI).Item("param_value").ToString
                                stlineinfo.ip = dt.Rows(CycI).Item("param_value").ToString
                        End Select
                End Select
            Next

            Me.cbJob.Items.Clear()
            '初始化Job列表
            Dim i As Long
            If Not m_Insight(CInt(lineid) - 1) Is Nothing Then
                ' Retrieve the file list from the sensor
                Dim oFileList() As String = m_Insight(CInt(lineid) - 1).File.GetFileList
                For i = 0 To UBound(oFileList)
                    ' Filter out non-job files
                    If UCase(Microsoft.VisualBasic.Right(oFileList(i), 4)) = ".JOB" Then
                        Me.cbJob.Items.Add(oFileList(i))
                    End If
                Next i
            End If
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Sub

    Private Sub saveLineInfo()
        saveLineInfoToDB()
        saveLineInfoToPLC()
        saveLineInfoToCamera()
        If (saveflag = 0) Then
            MsgBox("数据保存成功")
        End If
    End Sub

    '将数据保存到数据库中
    Private Sub saveLineInfoToDB()
        Dim funName As String = "saveLineInfoToDB"
        Dim sqlStr As String = ""
        Dim isright As Boolean = False
        Try
            '更新job参数
            If (stlineinfo.job <> Me.cbJob.Text) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.cbJob.Text + "' where param_type='job' and param_name='jobname' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新kickparam
            If (stlineinfo.kickparam <> Me.nudKickParam.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudKickParam.Value.ToString + "' where param_type='plc' and param_name='kickparam' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新kickparam
            If (stlineinfo.stackTopNum <> Me.nudStackTopNum.Value) Then
                
                sqlStr = "update t_lineinfo set param_value='" + Me.nudStackTopNum.Value.ToString + "' where param_type='onlinecounter' and param_name='stackTopNum' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then
                    stacktopsum(CInt(lineid) - 1) = Me.nudStackTopNum.Value
                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新cameraparam1
            If (stlineinfo.cameraparam1 <> Me.nudCameraParam1.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudCameraParam1.Value.ToString + "' where param_type='cameraparam' and param_name='param1' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新cameraparam2
            If (stlineinfo.cameraparam2 <> Me.nudCameraParam2.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudCameraParam2.Value.ToString + "' where param_type='cameraparam' and param_name='param2' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新cameraparam3
            If (stlineinfo.cameraparam3 <> Me.nudCameraParam3.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudCameraParam3.Value.ToString + "' where param_type='cameraparam' and param_name='param3' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If
            '更新cameraparam4
            If (stlineinfo.cameraparam4 <> Me.nudCameraParam4.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudCameraParam4.Value.ToString + "' where param_type='cameraparam' and param_name='param4' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新cameraparam5
            If (stlineinfo.cameraparam5 <> Me.nudCameraParam5.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudCameraParam5.Value.ToString + "' where param_type='cameraparam' and param_name='param5' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If
            '更新cameraparam6
            If (stlineinfo.cameraparam6 <> Me.nudCameraParam6.Value) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.nudCameraParam6.Value.ToString + "' where param_type='cameraparam' and param_name='param6' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If

            '更新cameraip
            If (stlineinfo.ip <> Me.txtCameraIP.Text) Then
                sqlStr = "update t_lineinfo set param_value='" + Me.txtCameraIP.Text + "' where param_type='cameraparam' and param_name='ip' and belong_lineid='" + lineid + "'"
                If (execSQLQueryNoResult(sqlStr)) Then

                Else
                    MsgBox("出错了")
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Sub

    '将数据保存到PLC中
    Private Function saveLineInfoToPLC() As Boolean
        Dim flag As Boolean = False
        If (stlineinfo.kickparam <> Me.nudKickParam.Value) Then
            If (updatePLCDate(kicknum(CInt(lineid) - 1), Me.nudKickParam.Value)) Then
                flag = True
            Else
                flag = False
            End If
        End If

        If (updatePLCDate(drsnum(CInt(lineid) - 1), Me.nudStackTopNum.Value)) Then
            flag = True
        Else
            flag = False
        End If
        Return flag

    End Function

    '将job信息保存到相机里面
    Private Sub saveLineInfoToCamera()
        Dim funName As String = "saveLineInfoToCamera"
        Try
            If (stlineinfo.job <> Me.cbJob.Text) Then '变化的才保存
                If Not m_Insight(0) Is Nothing Then

                    Dim bState As Boolean
                    ' Save current state
                    bState = m_Insight(0).SoftOnline
                    ' Must be offline to load a job
                    m_Insight(0).SoftOnline = False
                    ' Load the new job file
                    m_Insight(0).File.LoadJobFile(Me.cbJob.SelectedItem)
                    ' restore the online state to what it was
                    m_Insight(0).SoftOnline = True
                End If
            End If
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Sub
End Class